"""
数据库模型定义
"""
from sqlalchemy import Column, BigInteger, String, Text, Integer, Boolean, DateTime, ForeignKey, JSON, SmallInteger, DECIMAL
from sqlalchemy.orm import relationship
from datetime import datetime
from database.base import Base


class User(Base):
    """用户表"""
    __tablename__ = "users"
    
    id = Column(BigInteger, primary_key=True, index=True, comment="用户ID")
    username = Column(String(100), unique=True, nullable=False, index=True, comment="用户名")
    nickname = Column(String(100), nullable=False, comment="昵称")
    password_hash = Column(String(255), nullable=False, comment="密码哈希")
    email = Column(String(100), unique=True, index=True, comment="邮箱")
    phone = Column(String(50), unique=True, index=True, comment="手机号")
    avatar_url = Column(String(500), comment="头像URL")
    bio = Column(Text, comment="个人简介")
    gender = Column(SmallInteger, default=0, comment="性别：0-未知，1-男，2-女")
    birthday = Column(DateTime, comment="生日")
    location = Column(String(200), comment="地理位置")
    preferences = Column(JSON, comment="用户偏好设置")
    status = Column(SmallInteger, default=1, comment="状态：0-禁用，1-正常")
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关联关系
    contents = relationship("Content", back_populates="user", cascade="all, delete-orphan")
    interactions = relationship("Interaction", back_populates="user", cascade="all, delete-orphan")
    followers = relationship("Follow", foreign_keys="Follow.followed_id", back_populates="followed_user")
    following = relationship("Follow", foreign_keys="Follow.follower_id", back_populates="follower_user")
    interest_vector = relationship("UserInterestVector", back_populates="user", uselist=False, cascade="all, delete-orphan")
    user_interests = relationship("UserInterestTag", foreign_keys="UserInterestTag.user_id", cascade="all, delete-orphan")


class Content(Base):
    """内容表"""
    __tablename__ = "contents"
    
    id = Column(BigInteger, primary_key=True, index=True, comment="内容ID")
    user_id = Column(BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True, comment="发布用户ID")
    title = Column(String(200), comment="标题")
    content = Column(Text, nullable=False, comment="内容正文")
    media_type = Column(SmallInteger, nullable=False, default=1, index=True, comment="媒体类型：1-图文，2-视频，3-直播")
    media_urls = Column(JSON, comment="媒体文件URL列表")
    tags = Column(JSON, comment="标签列表")
    location = Column(String(200), comment="发布位置")
    view_count = Column(Integer, default=0, comment="浏览次数")
    like_count = Column(Integer, default=0, comment="点赞数")
    collect_count = Column(Integer, default=0, comment="收藏数")
    comment_count = Column(Integer, default=0, comment="评论数")
    share_count = Column(Integer, default=0, comment="分享数")
    status = Column(SmallInteger, default=1, index=True, comment="状态：0-待审核，1-正常，2-违规，3-删除")
    is_top = Column(Boolean, default=False, comment="是否置顶")
    is_original = Column(Boolean, default=True, comment="是否原创")
    publish_time = Column(DateTime, default=datetime.now, index=True, comment="发布时间")
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关联关系
    user = relationship("User", back_populates="contents")
    interactions = relationship("Interaction", back_populates="content", cascade="all, delete-orphan")
    vector = relationship("ContentVector", back_populates="content", uselist=False, cascade="all, delete-orphan")


class Interaction(Base):
    """互动表"""
    __tablename__ = "interactions"
    
    id = Column(BigInteger, primary_key=True, index=True, comment="互动ID")
    user_id = Column(BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True, comment="操作用户ID")
    content_id = Column(BigInteger, ForeignKey("contents.id", ondelete="CASCADE"), nullable=False, index=True, comment="内容ID")
    type = Column(String(20), nullable=False, index=True, comment="互动类型：like-点赞，collect-收藏，comment-评论")
    comment_content = Column(Text, comment="评论内容")
    parent_id = Column(BigInteger, ForeignKey("interactions.id", ondelete="CASCADE"), index=True, comment="父评论ID")
    created_at = Column(DateTime, default=datetime.now, index=True, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关联关系
    user = relationship("User", back_populates="interactions")
    content = relationship("Content", back_populates="interactions")
    parent = relationship("Interaction", remote_side=[id], backref="replies")


class Follow(Base):
    """关注关系表"""
    __tablename__ = "follows"
    
    id = Column(BigInteger, primary_key=True, index=True, comment="关系ID")
    follower_id = Column(BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True, comment="关注者ID")
    followed_id = Column(BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True, comment="被关注者ID")
    created_at = Column(DateTime, default=datetime.now, index=True, comment="创建时间")
    
    # 关联关系
    follower_user = relationship("User", foreign_keys=[follower_id], back_populates="following")
    followed_user = relationship("User", foreign_keys=[followed_id], back_populates="followers")


class SystemPrompt(Base):
    """系统提示词表"""
    __tablename__ = "system_prompts"
    
    id = Column(BigInteger, primary_key=True, index=True, comment="提示词ID")
    name = Column(String(100), unique=True, nullable=False, index=True, comment="提示词名称")
    scene = Column(String(50), nullable=False, index=True, comment="应用场景")
    prompt_template = Column(Text, nullable=False, comment="提示词模板")
    description = Column(String(500), comment="描述")
    is_active = Column(Boolean, default=True, comment="是否启用")
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")


class ContentVector(Base):
    """内容向量映射表"""
    __tablename__ = "content_vectors"
    
    id = Column(BigInteger, primary_key=True, index=True, comment="向量映射ID")
    content_id = Column(BigInteger, ForeignKey("contents.id", ondelete="CASCADE"), nullable=False, unique=True, index=True, comment="内容ID")
    vector_id = Column(String(100), nullable=False, unique=True, index=True, comment="向量库中的向量ID")
    content_text = Column(Text, comment="向量化的原文内容")
    embedding = Column(Text, comment="向量嵌入（JSON格式）")
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关联关系
    content = relationship("Content", back_populates="vector")


class UserInterestVector(Base):
    """用户兴趣向量映射表"""
    __tablename__ = "user_interest_vectors"
    
    id = Column(BigInteger, primary_key=True, index=True, comment="兴趣向量ID")
    user_id = Column(BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, unique=True, index=True, comment="用户ID")
    vector_id = Column(String(100), nullable=False, unique=True, index=True, comment="向量库中的向量ID")
    interest_tags = Column(JSON, comment="用户兴趣标签")
    interaction_score = Column(DECIMAL(10, 2), default=0.0, comment="互动活跃度评分")
    last_update_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="最后更新时间")
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    
    # 关联关系
    user = relationship("User", back_populates="interest_vector")


class IdentityVerification(Base):
    """实名认证表"""
    __tablename__ = "identity_verifications"
    
    id = Column(BigInteger, primary_key=True, index=True, comment="认证ID")
    user_id = Column(BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, unique=True, index=True, comment="用户ID")
    real_name = Column(String(100), nullable=False, comment="真实姓名")
    id_number = Column(String(18), nullable=False, comment="身份证号码")
    id_card_front_url = Column(String(500), comment="身份证正面照片URL")
    id_card_back_url = Column(String(500), comment="身份证背面照片URL")
    ocr_result = Column(JSON, comment="OCR识别结果（原始数据）")
    status = Column(SmallInteger, default=0, index=True, comment="审核状态：0-待审核，1-已通过，2-已拒绝")
    reject_reason = Column(String(500), comment="拒绝原因")
    verified_at = Column(DateTime, comment="审核通过时间")
    created_at = Column(DateTime, default=datetime.now, comment="提交时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关联关系
    user = relationship("User")

